package com.code.petapp.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.code.petapp.R;
import com.code.petapp.bean.OrderBean;
import com.code.petapp.databinding.ActivityBuyBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.OrderUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.Objects;

public class BuyActivity extends BaseActivity {
    ActivityBuyBinding binding;
    private int goodsId;
    private Float price;
    private String userId;
    private String username;
    private String phone;
    private String address;
    private int num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityBuyBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Intent intent = getIntent();
        goodsId=intent.getIntExtra("goodsId",0);
        price=Float.parseFloat(Objects.requireNonNull(intent.getStringExtra("price")));
        num=intent.getIntExtra("num",1);
        binding.price.setText(String.format("%.2f",price));
        setNum();
        binding.btnBuy.setOnClickListener(v -> {
            if(binding.name.getText().toString().isEmpty()||binding.address.getText().toString().isEmpty()||binding.phone.getText().toString().isEmpty()){
                toastShortShow("请填写完整信息");
            }else{
                binding.btnBuy.setEnabled(false);
                buy();
            }
        });

    }
    private void buy(){
        OrderBean orderBean=new OrderBean();
        orderBean.setUserId(SPCommonUtils.getInstance().getUserId());
        orderBean.setGoodsId(goodsId);
        orderBean.setUsername(binding.name.getText().toString());
        orderBean.setAddress(binding.address.getText().toString());
        orderBean.setPhone(binding.phone.getText().toString());
        orderBean.setNum(Integer.parseInt(binding.num.getText().toString()));
        orderBean.setPrice(Double.parseDouble(binding.price.getText().toString()));
        orderBean.setStatus("待发货");
        OrderUtils.add(SPCommonUtils.getInstance().getToken(), orderBean, new HttpListener<>() {
            @Override
            public void success(String data) {
                toastShortShow("下单成功");
                finish();
            }

            @Override
            public void error(int code, String error) {
                if(code==5006){
                    toastShortShow("账户余额不足，请先到个人中心充值");
                }
                toastShortShow(error);
            }
        });

    }
    private void setNum(){
        binding.reduce.setOnClickListener(v -> {
            int currentNum = Integer.parseInt(binding.num.getText().toString());
            if(currentNum>1){
                currentNum--;
                binding.num.setText(String.valueOf(currentNum));
                Float thisPrice=price*currentNum;
                binding.price.setText(String.format("%.2f",thisPrice));
            }
        });
        binding.add.setOnClickListener(v -> {
            int currentNum = Integer.parseInt(binding.num.getText().toString());
            if(currentNum<num){
                currentNum++;
                binding.num.setText(String.valueOf(currentNum));
                Float thisPrice=price*currentNum;
                binding.price.setText(String.format("%.2f",thisPrice));
            }
        });
    }
}